package com.kevin.data.jvm;

/**
 * 测试MinorGC后对象进入老年代
 *
 *
 * @author Kevin Liu
 * @see #testDynamicAgeJudge 动态年龄判断
 * @see #testSurvivorSpaceNotEnough GC后剩余对象大于Survivor小于老年代 直接进入老年代
 * @see #testOldObjEnterOld 大对象直接进入老年代
 * @see #testGcCount() 躲过15次GC直接进入老年代
 * @since 1.0.0
 */
public class EnterOldTest {

    public static void main(String[] args) {
        testOldObjEnterOld();
    }


    /**
     * 15次或者配置的GC次数进入老年代
     */
    public static void testGcCount(){
        byte[] arr1 = new byte[1024 * 1024 * 2];
        arr1 = new byte[1024 * 1024 * 2];
        arr1 = new byte[1024 * 1024 * 2];
        arr1 = null;
        byte[] arr2 = new byte[32 * 1024];
        arr1 = new byte[1024 * 1024 * 2];
        for (int i = 0; i < 15; i++) {
            arr1 = new byte[1024 * 1024 * 2];
            arr1 = new byte[1024 * 1024 * 2];
            arr1 = null;
            byte[] arr4 = new byte[32 * 1024];
            arr1 = new byte[1024 * 1024 * 2];
        }
    }

    /**
     * 测试GC对象大小大于Survivor的50%后进入老年代
     */
    public static void testDynamicAgeJudge() {
        byte[] arr1 = new byte[1024 * 1024 * 2];
        arr1 = new byte[1024 * 1024 * 2];
        arr1 = new byte[1024 * 1024 * 2];
        arr1 = null;

        byte[] arr2 = new byte[128 * 1024];

        byte[] arr3 = new byte[1024 * 1024 * 2];

        arr3 = new byte[1024 * 1024 * 2];

        arr3 = new byte[1024 * 1024 * 2];

        arr3 = new byte[128 * 1024*5];
        arr3 = null;

        byte[] arr4 = new byte[1024 * 1024*2];
    }

    /**
     * GC后剩余对象大于Survivor小于老年代 直接进入老年代
     */
    public static void testSurvivorSpaceNotEnough() {
        byte[] arr1 = new byte[1024 * 1024 * 2];
        arr1 = new byte[1024 * 1024 * 2];
        arr1 = new byte[1024 * 1024 * 2];
        byte[] arr2 = new byte[128 * 1024];
        arr2 = null;
        byte[]  arr3 = new byte[1024 * 1024 * 2];
    }

    /**
     * 测试大对象直接进入老年代
     */
    public static void testOldObjEnterOld(){
        byte[] arr1 = new byte[1024 * 1024 * 7];
        byte[] arr2 = new byte[1024 * 1024 * 2];
        arr2 = new byte[1024 * 1024 * 2];
        arr2 = new byte[1024 * 1024 * 2];
        arr2 = new byte[1024 * 1024 * 1];
        arr2 = null;
        byte[] arr3 = new byte[128 * 1024];

    }
}
